<template>
  <div>
    <TopNav title="我的关注" :bgc="$colorObj.color" />
    <div class="main">
      <div
        class="followsItem"
        v-for="(item, index) in followsList"
        :key="item.id"
      >
        <img
          v-if="item.head_img"
          :src="$axios.defaults.baseURL + item.head_img"
          alt=""
        />
        <img v-else src="@/assets/logo.png" alt="" />
        <div class="info">
          <div class="nickname">{{ item.nickname }}</div>
          <div class="date">{{ item.create_date.split("T")[0] }}</div>
        </div>
        <div
          :class="followsList[index].isFollows ? 'follows' : 'unfollows'"
          :style="[
            {
              background: !followsList[index].isFollows ? $colorObj.color : '',
            },
            {
              borderColor: !followsList[index].isFollows ? $colorObj.color : '',
            },
          ]"
          @click="handelFollows(index, item.id)"
        >
          {{ followsList[index].isFollows ? "取消关注" : "关注" }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import TopNav from "@/components/TopNav";
export default {
  components: {
    TopNav,
  },
  data() {
    return {
      followsList: [],
    };
  },
  created() {
    this.$axios({
      url: "/user_follows",
    }).then((res) => {
      const newData = res.data.data.map((item) => {
        return {
          ...item,
          isFollows: true,
        };
      });
      this.followsList = newData;
    });
  },
  methods: {
    handelFollows(index, id) {
      if (this.followsList[index].isFollows) {
        this.$axios({
          // 取消关注
          url: "/user_unfollow/" + id,
        }).then((res) => {
          console.log(res.data);
          this.followsList[index].isFollows = false;
        });
      } else {
        this.$axios({
          // 取消关注
          url: "/user_follows/" + id,
        }).then((res) => {
          console.log(res.data);
          this.followsList[index].isFollows = true;
        });
      }
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  padding-top: 46 / 360 * 100vw;
}
.followsItem {
  display: flex;
  padding: 12 / 360 * 100vw;
  align-items: center;
  border-bottom: 1px solid #e4e4e4;
  img {
    width: 50 / 360 * 100vw;
    height: 50 / 360 * 100vw;
    border-radius: 50%;
    object-fit: cover;
    margin-right: 10 / 360 * 100vw;
  }
  .info {
    flex: 1;
    .nickname {
      font-size: 16 / 360 * 100vw;
    }
    .date {
      font-size: 14 / 360 * 100vw;
      color: #888;
      margin-top: 4 / 360 * 100vw;
    }
  }
  .follows,
  .unfollows {
    width: 80 / 360 * 100vw;
    height: 30 / 360 * 100vw;
    line-height: 30 / 360 * 100vw;
    border: 1px solid #333;
    text-align: center;
    font-size: 14 / 360 * 100vw;
    border-radius: 15 / 360 * 100vw;
  }
  .unfollows {
    color: #fff;
  }
}
</style>
